//用数组来实现队列
//设计循环队列
class MyCircularQueue{
    public int [] elem;
    public int first;
    public  int last;

    public MyCircularQueue(int k) {
        elem=new int[k+1];
    }
    public boolean enQueue(int val){
        if (isFull()){
            return false;
        }
        elem[last]=val;
        last=(last+1)%elem.length;
        return true;

    }
    public boolean deQueue(){
        if (isEmpty()){
            return false;
        }
        first=(first+1)%elem.length;
        return true;

    }
    public boolean isEmpty(){
        return first==last;
    }
    public boolean isFull(){
        return (last+1)%elem.length==first;
    }
    //返回队头元素
    public int Front(){
        if (isEmpty()){
            return -1;
        }
        return elem[first];
    }
    //返回队尾元素
    public int rear(){
        if (isEmpty()){
            return -1;
        }
        int index=(last==0)?elem.length-1:last-1;
        return elem[index];
    }
}
public class Text3 {
    public static void main(String[] args) {
        MyCircularQueue myCircularQueue=new MyCircularQueue(8);
        myCircularQueue.enQueue(0);
        myCircularQueue.enQueue(1);
        myCircularQueue.enQueue(2);
        myCircularQueue.enQueue(3);
        myCircularQueue.enQueue(4);
        myCircularQueue.enQueue(5);
        myCircularQueue.enQueue(6);
        //System.out.println(myCircularQueue.Front());
        System.out.println(myCircularQueue.rear());
        myCircularQueue.enQueue(7);
        System.out.println(myCircularQueue.rear());
        myCircularQueue.enQueue(8);
        System.out.println(myCircularQueue.rear());


    }
}
